﻿/* ==============================================================================
 * 功能描述：SoundManager  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/4 19:47:41
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using ZZQFrameWork;

/// <summary>
/// Sound管理器
/// </summary>
public class SoundManager : Singleton<SoundManager>
{

    #region 音乐 && 音效 AudioSource

    private GameObject soundAudioManager;

    public GameObject SoundAudioManager
    {
        get
        {
            if (soundAudioManager == null)
            {
                soundAudioManager = UnityEngine.GameObject.Instantiate(ResManager.Instance.Load("SoundManager") as GameObject);
            }
            return soundAudioManager;
        }
    }

    private AudioSource musicAudioSource;

    public AudioSource MusicAudioSource
    {
        get
        {
            if (musicAudioSource == null)
            {
                GameObject obj = SoundAudioManager.transform.Find("MusicAudioSource").gameObject;
                if (obj == null)
                {
                    obj = new GameObject("MusicAudioSource");
                    obj.transform.parent = SoundAudioManager.transform;
                }
                musicAudioSource = obj.GetOrAddComponent<AudioSource>();
                musicAudioSource.playOnAwake = false;
                musicAudioSource.loop = true;
            }
            return musicAudioSource;
        }
    }

    private AudioSource effectAudioSource;

    public AudioSource EffectAudioSource
    {
        get
        {
            if (effectAudioSource == null)
            {
                GameObject obj = SoundAudioManager.transform.Find("EffectAudioSource").gameObject;
                if (obj == null)
                {
                    obj = new GameObject("EffectAudioSource");
                    obj.transform.parent = SoundAudioManager.transform;
                }
                effectAudioSource = obj.GetOrAddComponent<AudioSource>();
            }
            return effectAudioSource;
        }
    }

    #endregion

    #region 设置音乐 && 音效 声音大小

    public float MusicVolume
    {
        get { return MusicAudioSource.volume; }
        set { MusicAudioSource.volume = value; }
    }

    public float EffectVolume
    {
        get { return EffectAudioSource.volume; }
        set { EffectAudioSource.volume = value; }
    }

    #endregion

    #region 播放/停止 音乐 && 音效

    /// <summary>
    /// 播放音乐
    /// </summary>
    /// <param name="name">音乐名称</param>
    public void PlayMusic(string name)
    {
        Debug.Log(MusicAudioSource.name);
        if (MusicAudioSource.clip!=null&&MusicAudioSource.clip.name == name) return;
        AudioClip clip = ResManager.Instance.Load(name) as AudioClip;
        MusicAudioSource.clip = clip;
        MusicAudioSource.Play();
    }

    /// <summary>
    /// 停止音乐
    /// </summary>
    public void StopMusic()
    {
        MusicAudioSource.Stop();
    }

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="name">音效名称</param>
    public void PlayEffect(string name)
    {
        AudioClip clip = ResManager.Instance.Load(name) as AudioClip;
        EffectAudioSource.PlayOneShot(clip);
    }

    #endregion

}